package com.crm.miaohe.controller.information;

import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.information.request.InformationDeleteRequest;
import com.crm.miaohe.controller.information.request.InformationQueryRequest;
import com.crm.miaohe.controller.information.request.InformationRequest;
import com.crm.miaohe.model.TblInformation;
import com.crm.miaohe.service.InformationService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/erp/information")
public class InformationController {

    private final InformationService informationService;

    public InformationController(InformationService informationService) {
        this.informationService = informationService;
    }

    @PostMapping
    public ResponseEntity<Void> addInformation(@RequestBody @Valid InformationRequest request) {
        informationService.addInformation(request);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public ResponseEntity<Void> updateInformation(@RequestBody @Valid InformationRequest request) {
        informationService.updateInformation(request);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{informationId}")
    public ResponseEntity<Void> deleteInformation(@PathVariable Long informationId) {
        informationService.delete(informationId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/batch_delete")
    public ResponseEntity<Void> deleteInformation(@RequestBody @Valid InformationDeleteRequest request) {
        informationService.delete(request.getInformationIds());
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{informationId}")
    public ResponseEntity<TblInformation> queryInformation(@PathVariable Long informationId) {
        return ResponseEntity.ok(informationService.queryInformation(informationId));
    }

    @GetMapping("/list")
    public ResponseEntity<PageResponse<TblInformation>> queryInformation(@Valid InformationQueryRequest request) {
        return ResponseEntity.ok(informationService.queryInformation(request));
    }

}
